package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.ProfessDirectionDao;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ProfessDirectionActionImpl.java
* Description：专业方向actionImpl类,负责操作专业方向数据
* History：
* Date           Author               Desc
* 2017.4.12		  xzx                 建立
* -------------------------------------------------------
*/

@Component("professDirectionActionImpl")
@Transactional
public class ProfessDirectionActionImpl {

	@Resource(name="professDirectionDao")
	private ProfessDirectionDao professDirectionDao;
	
	@Resource(name = "departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	/**
	 * @description 添加专业方向
	 * @param Map<String,Object> paramMap  专业方向名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> addDirect(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		int trueLength = 0;
		try {
			//得到专业方向名字
			List<String> directList = (List<String>)paramMap.get("directions");
			if(directList.size() != 0){
				List<Map<String, Object>> directListUse;
				Map<String, Object> mapUse;
				for(int i = 0; i < directList.size(); i++){
					mapUse = new HashMap<String, Object>();
					mapUse.put("directName", directList.get(i));
					directListUse = professDirectionDao.getDirectByName(mapUse);
					//判断重复名
					if(directListUse != null && directListUse.size() != 0){
						break;
					}else{
						trueLength += 1;
					}
				}
				if(trueLength == directList.size()){
					//添加方向
					professDirectionDao.addDirect(paramMap);
					resultMap.put("result", true);
				}else{
					resultMap.put("result", false);
					resultMap.put("failMsg", "专业方向名称重复");
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 修改专业方向
	 * @param Map<String,Object> paramMap  专业方向名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveDirect(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
			List<Map<String, Object>> directList = professDirectionDao.getDirectByName(paramMap);
			//判断重复
			if(directList != null && directList.size() != 0){
				failMsg = "该专业方向名已存在";
			}else{
				//修改专业方向
				professDirectionDao.saveDirect(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("errorMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 删除专业方向
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public boolean delDirect(Map<String, Object> paramMap) throws Exception{
		
		boolean result = false;
		try{
			//删除专业方向信息
			professDirectionDao.delDirect(paramMap);
			result = true;
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 查询某个专业的专业方向列表
	 * @param Map<String,Object> paramMap  专业id
	 * @return List<Map<String, String>> 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getDirectByprofessId(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = professDirectionDao.getDirectByprofessId(paramMap);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	
	/**
	 * @description 修改专业的专业方向
	 * @param Map<String,Object> paramMap  专业方向名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> saveDirectList(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> paramDirectList = (List<Map<String, Object>>)paramMap.get("directions");
			List<Map<String, Object>> directList;
			for(int i = 0; i < paramDirectList.size(); i++){
				paramDirectList.get(i).put("professionId", paramMap.get("professionId"));
				//新增
				if(Integer.parseInt(paramDirectList.get(i).get("mainId").toString()) == 0){
					directList = professDirectionDao.getDirectByName(paramDirectList.get(i));
					if(directList != null && directList.size() != 0 ){
						resultMap.put("result", false);
						resultMap.put("failMsg", "专业方向名称重复");
						break;
					}else{
						professDirectionDao.addDirectOne(paramDirectList.get(i));
						resultMap.put("result", true);
					}
				//修改	
				}else{
					List<Map<String, Object>> directListSave = professDirectionDao.getDirectByName(paramDirectList.get(i));
					if(directListSave != null && directListSave.size() != 0 ){
						resultMap.put("result", false);
						resultMap.put("failMsg", "专业方向名称重复");
						break;
					}else{
						professDirectionDao.saveDirect(paramDirectList.get(i)); 
						resultMap.put("result", true);
					}
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
}